﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.SessionState;
using CongThongTin.Util;
using System.Linq;
using CongThongTin.Entities;

namespace CongThongTin
{
    /// <summary>
    /// Summary description for ImageHandler
    /// </summary>
    public class ImageHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            CongThongTinEntities _entities=new CongThongTinEntities ();
           
            int option = Convert.ToInt32(context.Request.Params["option"].ToString());
           
            context.Response.Clear();

            if(option==0)
            {
                var storedImage = context.Session[cGlobalUtil.STORED_IMAGE] as byte[];
                if (storedImage != null)
                {
                    Image image = GetImage(storedImage);
                    if (image != null)
                    {
                        context.Response.ContentType = "image/jpeg";
                        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                    }
                }   
            }
            if (option == 1)
            {
                int maTheLoai=_entities.THELOAIs.SingleOrDefault(tl=>tl.TENTHELOAI.Equals("Tin tức")).MATHELOAI;
                int maLoaiTin=_entities.LOAITINs.SingleOrDefault(l=>l.TENLOAITIN.Equals("")).MALOAITIN;

                var vTinTuc = (from tt in _entities.TINTUCs
                               where tt.MATHELOAI == maTheLoai
                               && tt.MALOAITINTUC == maLoaiTin
                               select tt).Take(1);

                foreach(var item in vTinTuc)
                {
                    var storedImage =item.HINHMOTA.ToArray();
                    if (storedImage != null)
                    { 
                        Image image = GetImage(storedImage);
                        if (image != null)
                        {
                            context.Response.ContentType = "image/jpeg";
                            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                        }
                    }   
                }
            }
            if (option == 2)
            { 
                int maTinTuc=Convert.ToInt32(context.Request.Params["id"].ToString());
                Entities.TINTUC oTinTuc = _entities.TINTUCs.SingleOrDefault(t => t.MATINTUC == maTinTuc);

                var storedImage = oTinTuc.HINHMOTA.ToArray();
                if (storedImage != null)
                {
                    Image image = GetImage(storedImage);
                    if (image != null)
                    {
                        context.Response.ContentType = "image/jpeg";
                        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                    }
                }   
            }
            
        }

        private Image GetImage(byte[] storedImage)
        {
            var stream = new MemoryStream(storedImage);
            return Image.FromStream(stream);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}